package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Book;
import model.Chapter;
import model.MySql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by yvdedu.com on 2016/10/14.
 */
@WebServlet(urlPatterns = "/getchapter_bybookid")
public class getchapter_bybookid extends HttpServlet{

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String book_id=req.getParameter("book_id");
        MySql mySql=new MySql();
        List<Chapter> chapter=  mySql.getChaptersByBook_id(Integer.parseInt(book_id));
        if (chapter!=null){
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(chapter);
            resp.getWriter().print(json);
        }
    }
}
